﻿@using Website.CRUDServices
@using Website.Models.Styling
@using Location = Website.Models.Domain.Location
@model Website.Models.ViewModels.ListView<Matchmaking>
@{
    this.ViewBag.Title = "Matchmaking";
    this.Layout = "~/Views/_Shared/_Layout.cshtml";
    var partners = this.ViewData["Partners"] as List<MatchmakingPartner> ?? new List<MatchmakingPartner>();
    var partnerTypes = this.ViewData["PartnerTypes"] as List<MatchmakingPartnerType> ?? new List<MatchmakingPartnerType>();
    var locations = this.ViewData["Locations"] as List<Location> ?? new List<Location>();
    var funds = this.ViewData["Funds"] as List<Fund> ?? new List<Fund>();
    var criteriaFunds = this.ViewData["CriteriaFunds"] as List<Guid> ?? new List<Guid>();
    DefaultSearchFields defaultFields = this.ViewData["DefaultFields"] as DefaultSearchFields ?? new DefaultSearchFields();
    var criteriaLocation = this.ViewData["CriteriaLocation"] as Guid?;
    var criteriaPartnerType = this.ViewData["CriteriaPartnerType"] as Guid?;
}
<h2>Søgeresultat - @ViewBag.Title</h2>

<fieldset class="searchCriterias">
    <legend>Søgning</legend>
    <table>    
        <tr>
            <td>Type:</td><td>@ViewBag.Title</td>
        </tr>
        <tr><td>Antal resultater i alt: </td><td>@Model.TotalRows</td></tr>
        @if (criteriaLocation.HasValue && criteriaLocation.Value != Guid.Empty)
        {
            <tr><td>Lokation: </td><td>@locations.Single(x => x.ID == criteriaLocation.Value).Name</td></tr>
        }
        @if (criteriaFunds.Any())
        {
            <tr><td>Fonde og puljer: </td>
                <td>
                    @foreach (Fund fund in funds.Where(x => criteriaFunds.Contains(x.ID)).OrderBy(x => x.Name))
                    {
                        @fund.Name
                        <br />
                    }
                </td>
            </tr>
        }
        @if (criteriaPartnerType.HasValue && criteriaPartnerType.Value != Guid.Empty)
        {
            <tr>
                <td>Samarbejdspartner</td>
                <td>@partnerTypes.Single(x => x.ID == criteriaPartnerType.Value).Name</td>
            </tr>
        }
        @Html.Partial("~/Views/Reports/_SearchDefaultFieldsParametersTableTR.cshtml", defaultFields)

    </table>
</fieldset>


<table class="tableView">
    <tr>
        @foreach (TableViewHeaderItem item in this.Model.Header.Items)
        {
            <td>
                @Html.Partial("~/Views/_Shared/TableViewHeader.cshtml", item)
            </td>
        }
        <td></td>
    </tr>
    @if (this.Model != null)
    {
        foreach (Matchmaking mm in this.Model.List)
        {
            List<Location> locs = locations.Where(x => x.ID == mm.Location).ToList();
            Location location = locs.Any() ? locations[0] : null;
            <tr>
                <td>@string.Format("{0:dd-MM-yyyy}", mm.Date)</td>
                <td>
                    @if (location != null)
                    {
                        @location.Name
                    }
                </td>
                <td>@mm.Name</td>

                <td>
                    @if (mm.Partners != null && mm.Partners.Count() > 0)
                    {
                        Matchmaking mm1 = mm;
                        var mmPartners = partners.Where(x => mm1.Partners.Contains(x.ID)).Select(x => x.Type);
                        @Html.Raw(string.Join(", ", partnerTypes.Where(x => mmPartners.Contains(x.ID)).Select(x => x.Name)))
                    }
                </td>
            
                <td>
                    @mm.Comments
                </td>
            </tr>
        }
        <tr><td colspan="5"></td></tr>
    }
    </table>